// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.figures;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.ui.gef.SimpleHtmlFigure;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.designer.ui.process.models.CompNodeStatus;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.geometry.Dimension;

/**
 * This class create a figure with the given image. <br/>eh
 * 
 * 
 */
public class CompNodeStatusFigure extends Figure {

    private final ImageFigure error;

    private final ImageFigure info;

    private final ImageFigure warning;

    private final SimpleHtmlFigure hint;

    private final CompNodeStatus nodeStatus;

    private final Dimension warningSize;

    private final Dimension infoSize;

    public CompNodeStatusFigure(CompNodeStatus nodeStatus) {
        this.nodeStatus = nodeStatus;

        this.setLayoutManager(new FreeformLayout());
        error = new ImageFigure();
        error.setImage(ImageUtil.getImage(IDenguesSharedImage.NODE_STATUS_ERROR));
        error.setVisible(false);
        error.setSize(error.getPreferredSize());
        add(error);

        warning = new ImageFigure();
        warning.setImage(ImageUtil.getImage(IDenguesSharedImage.NODE_STATUS_WARNING));
        warning.setVisible(false);
        warning.setSize(error.getPreferredSize());
        add(warning);
        warningSize = new Dimension(warning.getImage().getImageData().width, warning.getImage().getImageData().height);

        info = new ImageFigure();
        info.setImage(ImageUtil.getImage(IDenguesSharedImage.NODE_STATUS_INFO));
        info.setVisible(false);
        info.setSize(error.getPreferredSize());
        add(info);
        infoSize = new Dimension(info.getImage().getImageData().width, info.getImage().getImageData().height);

        hint = new SimpleHtmlFigure();
        updateStatus(nodeStatus.getType());
    }

    public void updateStatus(int type) {
        switch (type) {
        case CompNodeStatus.STATUS_NORMAL:
            error.setVisible(false);
            warning.setVisible(false);
            info.setVisible(false);
            setToolTip(null);
            break;
        case CompNodeStatus.STATUS_ERROR:
            error.setVisible(true);
            warning.setVisible(false);
            info.setVisible(false);
            hint.setText(nodeStatus.getMessage());
            setToolTip(hint);
            break;
        case CompNodeStatus.STATUS_WARNING:
            error.setVisible(false);
            warning.setVisible(true);
            info.setVisible(false);
            hint.setText(nodeStatus.getMessage());
            setToolTip(hint);
            break;
        case CompNodeStatus.STATUS_INFO:
            error.setVisible(false);
            warning.setVisible(false);
            info.setVisible(true);
            hint.setText(nodeStatus.getMessage());
            setToolTip(hint);
            break;
        }
    }

    public Dimension getCustomSize() {
        return warningSize;
    }
}
